package com.csjbot.weedle_auto.util

import android.annotation.SuppressLint
import android.annotation.TargetApi
import android.content.Context
import android.content.SharedPreferences
import android.content.SharedPreferences.Editor
import android.os.Build
import android.text.TextUtils

import com.alibaba.fastjson.JSON

/**
 * @ClassName: SharePreferenceUtils
 * @Description: SharePreference工具类
 * @Author：lxs
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
class SharePreferenceTools @SuppressLint("CommitPrefEdits")
constructor(context: Context, preferenceName: String, mode: Int) {

    private val sharedPreferences: SharedPreferences = context.getSharedPreferences(preferenceName, mode)
    private val editor: Editor
    private val ZERO = 0

    constructor(context: Context) : this(context, PREFERENCE_DEFAULT_NAME, Context.MODE_PRIVATE)

    constructor(context: Context, preferenceName: String) : this(context, preferenceName, Context.MODE_PRIVATE)

    init {
        editor = sharedPreferences.edit()
    }

    fun putString(key: String, value: String) {
        if (!TextUtils.isEmpty(key) && !TextUtils.isEmpty(value)) {
            editor.putString(key, value)
            editor.commit()
        }
    }

    fun getString(key: String): String? {
        var value: String? = null
        if (!TextUtils.isEmpty(key)) {
            value = sharedPreferences.getString(key, null)
        }
        return value
    }

    fun getString(key: String, defaultValue: String): String? {
        var value: String? = null
        if (!TextUtils.isEmpty(key)) {
            value = sharedPreferences.getString(key, defaultValue)
        }
        return value
    }

    fun putInt(key: String, value: Int) {
        if (!TextUtils.isEmpty(key)) {
            editor.putInt(key, value)
            editor.commit()
        }
    }


    fun getInt(key: String): Int {
        var value = ZERO
        if (!TextUtils.isEmpty(key)) {
            value = sharedPreferences.getInt(key, ZERO)
        }
        return value
    }

    fun getInt(key: String, defaultValue: Int): Int {
        var value = defaultValue
        if (!TextUtils.isEmpty(key)) {
            value = sharedPreferences.getInt(key, defaultValue)
        }
        return value
    }

    fun putBoolean(key: String, value: Boolean) {
        if (!TextUtils.isEmpty(key)) {
            editor.putBoolean(key, value)
            editor.commit()
        }
    }

    fun getBoolean(key: String): Boolean {
        var result = false
        if (!TextUtils.isEmpty(key)) {
            result = sharedPreferences.getBoolean(key, false)
        }
        return result
    }

    fun getBoolean(key: String, defaultValue: Boolean): Boolean {
        var result = defaultValue
        if (!TextUtils.isEmpty(key)) {
            result = sharedPreferences.getBoolean(key, defaultValue)
        }
        return result
    }

    fun putLong(key: String, value: Long) {
        if (!TextUtils.isEmpty(key)) {
            editor.putLong(key, value)
            editor.commit()
        }
    }

    fun getLong(key: String): Long {
        var value = ZERO.toLong()
        if (!TextUtils.isEmpty(key)) {
            value = sharedPreferences.getLong(key, ZERO.toLong())
        }
        return value
    }

    fun getLong(key: String, defaultValue: Long): Long {
        var value = defaultValue
        if (!TextUtils.isEmpty(key)) {
            value = sharedPreferences.getLong(key, defaultValue)
        }
        return value
    }

    fun putFloat(key: String, value: Float) {
        if (!TextUtils.isEmpty(key)) {
            editor.putFloat(key, value)
            editor.commit()
        }
    }

    fun getFloat(key: String): Float {
        var value = ZERO.toFloat()
        if (!TextUtils.isEmpty(key)) {
            value = sharedPreferences.getFloat(key, ZERO.toFloat())
        }
        return value
    }

    fun getFloat(key: String, defaultValue: Float): Float {
        var value = defaultValue
        if (!TextUtils.isEmpty(key)) {
            value = sharedPreferences.getFloat(key, defaultValue)
        }
        return value
    }

    fun putStringSet(key: String, value: Set<String>) {
        if (!TextUtils.isEmpty(key)) {
            editor.putStringSet(key, value)
            editor.commit()
        }
    }

    fun getStringSet(key: String): Set<String>? {
        var set: Set<String>? = null
        if (!TextUtils.isEmpty(key)) {
            set = sharedPreferences.getStringSet(key, null)
        }
        return set
    }

    fun getStringSet(key: String, defaultValue: Set<String>): Set<String> {
        var set: Set<String>? = defaultValue
        if (!TextUtils.isEmpty(key)) {
            set = sharedPreferences.getStringSet(key, defaultValue)
        }
        return set!!
    }

    /**
     * 添加对象
     *
     * @param key
     * @param t
     */
    fun <T> putModel(key: String, t: T?) {
        if (!TextUtils.isEmpty(key) && t != null) {
            putString(key, JSON.toJSONString(t))// fastjson
        }
    }

    /**
     * 获取对象
     *
     * @param key
     * @param t
     * @return
     */
    fun <T> getModel(key: String, clazz: Class<T>): T? {
        var value: String? = null
        if (!TextUtils.isEmpty(key)) {
            value = getString(key)
        }
        return if (TextUtils.isEmpty(value)) null else JSON.parseObject(value, clazz)// fastjson
    }

    /**
     * 添加集合
     *
     * @param key
     * @param t
     */
    fun <T> putModels(key: String, t: List<T>?) {
        if (!TextUtils.isEmpty(key) && t != null && t.size > ZERO) {
            putString(key, JSON.toJSONString(t))// fastjson
        }
    }

    /**
     * 获取集合
     *
     * @param key
     * @param t
     * @return
     */
    fun <T> getModels(key: String, clazz: Class<T>): List<T>? {
        var value: String? = null
        if (!TextUtils.isEmpty(key)) {
            value = getString(key)
        }
        return if (TextUtils.isEmpty(value)) null else JSON.parseArray(value, clazz)// fastjson
    }

    /**
     * 查询某个key是否已经存在
     *
     * @param key
     * @return
     */
    operator fun contains(key: String): Boolean {
        return sharedPreferences.contains(key)
    }

    /**
     * 移除某个key值已经对应的值
     *
     * @param key
     */
    fun remove(key: String) {
        editor.remove(key)
        editor.commit()
    }

    /**
     * 清除所有数据
     */
    fun clear() {
        editor.clear()
        editor.commit()
    }

    /**
     * 返回所有的键值对
     *
     * @return
     */
    val all: Map<String, *>
        get() = sharedPreferences.all

    companion object {
        private val PREFERENCE_DEFAULT_NAME = "SnowBot"
    }
}
